package cn.demo.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一返回
 *
 * @author ShiXian
 * @version 1.0
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public final class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private boolean success;

    private int code;

    private String message;

    private T data;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long total;

    /**
     * 返回结果集，私有方法
     *
     * @param success 成功 true，异常 false
     * @param code    代码
     * @param data    数据
     * @param total   总条数
     * @return Response
     */
    private static <T> Response<T> restResult(boolean success, int code, String msg, T data, Long total) {
        Response<T> response = new Response<>();
        response.setSuccess(success);
        response.setCode(code);
        response.setMessage(msg);
        response.setData(data);
        response.setTotal(total);
        return response;
    }

    /**
     * 成功提示
     *
     * @return Response
     */
    public static <T> Response<T> success() {
        RespStatus status = RespStatus.SUCCESS;
        return restResult(true, status.getCode(), status.getMsg(), null, null);
    }

    /**
     * 成功提示
     *
     * @param msg 提示消息
     * @return Response
     */
    public static <T> Response<T> success(String msg) {
        return restResult(true, RespStatus.SUCCESS.getCode(), msg, null, null);
    }

    /**
     * 数据
     *
     * @param data 数据
     * @return Response
     */
    public static <T> Response<T> data(T data) {
        RespStatus status = RespStatus.SUCCESS;
        return restResult(true, status.getCode(), status.getMsg(), data, null);
    }

    /**
     * 数据
     *
     * @param data  数据
     * @param total 条数
     * @return
     */
    public static <T> Response<T> data(T data, long total) {
        RespStatus status = RespStatus.SUCCESS;
        return restResult(true, status.getCode(), status.getMsg(), data, total);
    }

    /**
     * 失败提示
     *
     * @return Response
     */
    public static <T> Response<T> failure() {
        RespStatus status = RespStatus.FAIL;
        return restResult(false, status.getCode(), status.getMsg(), null, null);
    }

    /**
     * 失败提示
     *
     * @param msg 提示信息
     * @return Response
     */
    public static <T> Response<T> failure(String msg) {
        return restResult(false, RespStatus.FAIL.getCode(), msg, null, null);
    }

    /**
     * 失败提示
     *
     * @param code 错误代码
     * @param msg  提示信息
     * @return Response
     */
    public static <T> Response<T> failure(int code, String msg) {
        return restResult(false, code, msg, null, null);
    }

    /**
     * 失败提示
     *
     * @param httpStatus 状态码
     * @return Response
     */
    public static <T> Response<T> failure(@NonNull RespStatus httpStatus) {
        return restResult(false, httpStatus.getCode(), httpStatus.getMsg(), null, null);
    }
}
